﻿namespace Arms.Persistence
{
    using Arms.Domain.Modules;
using Arms.Domain.Modules.Common;
using Arms.Domain.Modules.DiningRoom;
using Arms.Domain.Modules.Geography;
using Arms.Domain.Modules.Orders;
using Arms.Domain.Modules.People;
using Arms.Domain.Modules.Products;
using Arms.Persistence.Base.Conventions;
using Arms.Persistence.Modules.Common.Mappings;
using Arms.Persistence.Modules.Geography.Mappings;
using Arms.Persistence.Modules.Products.Mappings;
using System.Data.Entity;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;

    public class ArmsContext : DbContext, IDbContext
    {
        public ArmsContext():base("ArmsContext") { }

        #region DbSets

        #region Common

        public DbSet<AppConfig> AppConfigs { get; set; }
        public DbSet<Contact> Contacts { get; set; }


        #endregion

        #region Dining Room

        public DbSet<DiningRoomLayout> DiningRoomLayouts { get; set; }
        public DbSet<DiningRoomFloor> DiningRoomFloors { get; set; }
        public DbSet<DiningRoomTable> DiningRoomTables { get; set; }

        #endregion

        #region Geography

        public DbSet<City> Cities { get; set; }
        public DbSet<Country> Countries { get; set; }
        public DbSet<Place> Places { get; set; }
        public DbSet<PlaceType> PlaceTypes { get; set; }
        public DbSet<Point> Points { get; set; }
        public DbSet<Province> Provinces { get; set; }

        #endregion

        #region Orders

        public DbSet<Order> Orders { get; set; }
        public DbSet<OrderHeader> OrderHeaders { get; set; }
        public DbSet<Invoice> Invoices { get; set; }

        #endregion

        #region Products

        public DbSet<Menu> Menus { get; set; }
        public DbSet<MenuCategory> MenuCategories { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<ProductClass> ProductClasses { get; set; }
        public DbSet<ProductCombination> ProductCombinations { get; set; }
        public DbSet<ProductGroup> ProductGroups { get; set; }
        public DbSet<ProductSubClass> ProductSubClasses { get; set; }

        #endregion

        #region People

        public DbSet<User> Users { get; set; }
        public DbSet<UserClass> UserClasses { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<RoleGroup> RoleGroups { get; set; }
        public DbSet<Person> People { get; set; }
        public DbSet<Employee> Employees { get; set; }

        #endregion

        #endregion

        #region IDbContext Members

        public ObjectContext ObjectContext
        {
            get { return ((IObjectContextAdapter)this).ObjectContext; }
        }

        #endregion

        #region ModelBuilder

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
            //.Where(type => !String.IsNullOrEmpty(type.Namespace))
            //.Where(type => type.FindBaseType() != null && type.FindBaseType().IsGenericType
            //    && (type.FindBaseType().GetGenericTypeDefinition() == typeof(StructuralTypeConfiguration<>)));
            //foreach (var type in typesToRegister)
            //{
            //    dynamic configurationInstance = Activator.CreateInstance(type);
            //    modelBuilder.Configurations.Add(configurationInstance);
            //}

            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Conventions.Add<EntityNamingConvention>();

            modelBuilder.Configurations.Add(new ContactMap());

            modelBuilder.Configurations.Add(new CityMap());
            modelBuilder.Configurations.Add(new CountryMap());
            modelBuilder.Configurations.Add(new PointMap());
            modelBuilder.Configurations.Add(new PlaceMap());
            modelBuilder.Configurations.Add(new PlaceTypeMap());
            modelBuilder.Configurations.Add(new ProvinceMap());

            modelBuilder.Configurations.Add(new MenuMap());
            modelBuilder.Configurations.Add(new MenuCategoryMap());
            modelBuilder.Configurations.Add(new ProductClassMap());
            modelBuilder.Configurations.Add(new ProductCombinationMap());
            modelBuilder.Configurations.Add(new ProductGroupMap());
            modelBuilder.Configurations.Add(new ProductMap());
            modelBuilder.Configurations.Add(new ProductSubClassMap());

            base.OnModelCreating(modelBuilder);
        }

        #endregion
    }
}
